package com.nika.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nika.usercenter.exception.BusinessException;
import com.nika.usercenter.mapper.UserMapper;
import com.nika.usercenter.model.domain.Team;
import com.nika.usercenter.model.domain.User;
import com.nika.usercenter.model.request.TeamAddRequest;
import com.nika.usercenter.model.request.TeamJoinRequest;
import com.nika.usercenter.model.request.TeamQuitRequest;
import com.nika.usercenter.model.request.TeamUpdateRequest;
import com.nika.usercenter.model.vo.TeamVO;
import com.nika.usercenter.service.TeamService;
import com.nika.usercenter.service.UserService;
import common.BaseResponse;
import common.ErrorCode;
import common.ResultUtils;
import com.nika.usercenter.model.dto.TeamQuery;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

//这个注解让返回值都是json形式
@Api(tags = "队伍模块")
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TeamService teamService;


    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        if (teamAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUserByToken(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH, "用户未登录,无法添加队伍");
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest, team);
        long addTeamResult = teamService.addTeam(team, loginUser);
        if (addTeamResult < 0) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "插入失败");
        }
        return ResultUtils.success(addTeamResult);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> delTeam(@RequestBody Long id, HttpServletRequest request) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean del = teamService.delTeam(id,loginUser);
        if (!del) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest team, HttpServletRequest request) {
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean result = teamService.updateTeam(team, loginUser);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return ResultUtils.success(true);
    }

    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "没有查到该队伍");
        }
        return ResultUtils.success(team);
    }

    @GetMapping("/list")
    public BaseResponse<List<TeamVO>> listTeams(TeamQuery teamQuery, HttpServletRequest request) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "查询对象为空");
        }
        User loginUser = userService.getLoginUser(request);
        List<TeamVO> teamVOList = teamService.query(teamQuery, loginUser);
        return ResultUtils.success(teamVOList);
    }

    @GetMapping("/list/page")
    public BaseResponse<Page<Team>> listTeamsByPage(TeamQuery teamQuery) {
        if (teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "查询对象为空");
        }
        Team team = new Team();
        BeanUtils.copyProperties(team, teamQuery);
        Page<Team> page = new Page(teamQuery.getPageNum(), teamQuery.getPageSize());
        QueryWrapper<Team> teamQueryWrapper = new QueryWrapper<>(team);
        Page<Team> pageRes = teamService.page(page, teamQueryWrapper);
        return ResultUtils.success(pageRes);
    }

    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null || request == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        boolean result = teamService.joinTeam(teamJoinRequest, loginUser);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "加入队伍失败");
        }
        return ResultUtils.success(true);
    }

    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null || teamQuitRequest.getId() == null || teamQuitRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_NULL_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        boolean quitResult = teamService.quit(teamQuitRequest, loginUser);
        return ResultUtils.success(quitResult);
    }


}
